﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class UserInfoService : IUserInfoService
    {
        private IUsersInfoDal _usersInfo;
        private IMapper _mapper;
        private IUsersDal _usersDal;
        public UserInfoService(IUsersInfoDal usersInfo, IMapper mapper, IUsersDal usersDal)
        {
            _usersInfo = usersInfo;
            _mapper = mapper;
            _usersDal = usersDal;
        }

        public async Task<int> AddUserInfoAsync(string realName, DateTime birthday, int gender, string identityCard, string phone, string email, string address, int userId)
        {
            return await _usersInfo.AddAsync(new Usersinfo 
            {
                RealName = realName,
                Birthday = birthday,
                Gender = gender,
                IdentityCard = identityCard,
                Phone = phone,
                Email = email,
                Address = address,
                UsersId = userId
            });
        }

        public async Task<int> DeleteUserInfoAsync(int id)
        {
            var data = await _usersInfo.QueryAsync(id);
            return data == null ? -1 : await _usersInfo.DeleteAsync(data);
        }

        public async Task<int> EditUserInfoAsync(int id, string realName, DateTime birthday, int gender, string identityCard, string phone, string email, string address)
        {
            var data = await _usersInfo.QueryAsync(id);
            if (data == null)
                return -1;
            data.RealName = realName;
            data.Birthday = birthday;
            data.Gender = gender;
            data.IdentityCard = identityCard;
            data.Phone = phone;
            data.Email = email;
            data.Address = address;
            return await _usersInfo.EditAsync(data);
        }

        public async Task<List<UsersInfoDto>> QueryUserInfoAllAsync()
        {
            var data = await _usersInfo.QueryAsync();
            return _mapper.Map<List<Usersinfo>,List<UsersInfoDto>>(data);
        }

        public async Task<UsersInfoDto> QueryUserInfoByIdAsync(int id)
        {
            var data = await _usersInfo.QueryAsync(id);
            return _mapper.Map<Usersinfo,UsersInfoDto>(data);
        }

        public async Task<List<UsersInfoDto>> QueryUserInfoByNameAsync(string realName)
        {
            var data = await _usersInfo.QueryAsync(uf => uf.RealName.Equals(realName));
            return _mapper.Map<List<Usersinfo>, List<UsersInfoDto>>(data);
        }
    }
}
